package com.fan.shop.dao;

import java.util.List;

import javax.inject.Named;

import org.hibernate.SessionFactory;

import com.fan.shop.domain.Store;

@Named("storeDao")
public class StoreDaoImpl implements StoreDao {

	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Store getStore(String storecode) {
		Store store = (Store) sessionFactory.getCurrentSession().get(
				Store.class, storecode);
		return store;
	}

	@SuppressWarnings("unchecked")
	public List<Store> getAllStores() {
		List<Store> list = sessionFactory.getCurrentSession()
				.createCriteria(Store.class).list();
		return list;
	}

	public int getSize() {
		return getAllStores().size();
	}

	public void addStore(Store store) {
		sessionFactory.getCurrentSession().save(store);

	}

	public void updateStore(Store store) {
		sessionFactory.getCurrentSession().update(store);

	}

	public void deleteStore(Store store) {
		sessionFactory.getCurrentSession().delete(store);

	}

	public boolean hasStore(String storecode) {
		if (getStore(storecode) == null) {
			return false;
		} else {
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	public List<String> getColumnNames() {
		List<String> columns = sessionFactory
				.getCurrentSession()
				.createSQLQuery(
						"SELECT COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'STORE'")
				.list();

		return columns;
	}

}
